package com.letian.web.controller.record;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Time;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.letian.common.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.letian.common.annotation.Log;
import com.letian.common.core.controller.BaseController;
import com.letian.common.core.domain.AjaxResult;
import com.letian.common.enums.BusinessType;
import com.letian.record.domain.RecordOrder;
import com.letian.record.service.IRecordOrderService;
import com.letian.common.utils.poi.ExcelUtil;
import com.letian.common.core.page.TableDataInfo;

/**
 * 记录订单Controller
 *
 * @author bmz
 * @date 2023-07-20
 */
@RestController
@RequestMapping("/record/order")
public class RecordOrderController extends BaseController {
    @Autowired
    private IRecordOrderService recordOrderService;

    /**
     * 查询记录订单列表
     */
    @PreAuthorize("@ss.hasPermi('record:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(RecordOrder recordOrder) {
        startPage();
        List<RecordOrder> list = recordOrderService.selectRecordOrderList(recordOrder);
        return getDataTable(list);
    }

    /**
     * 导出记录订单列表
     */
    @PreAuthorize("@ss.hasPermi('record:order:export')")
    @Log(title = "记录订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RecordOrder recordOrder) {
        List<RecordOrder> list = recordOrderService.selectRecordOrderList(recordOrder);
        ExcelUtil<RecordOrder> util = new ExcelUtil<RecordOrder>(RecordOrder.class);
        util.exportExcel(response, list, "记录订单数据");
    }

    /**
     * 获取记录订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('record:order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId) {
        return success(recordOrderService.selectRecordOrderByOrderId(orderId));
    }

    /**
     * 新增记录订单
     */
    @PreAuthorize("@ss.hasPermi('record:order:add')")
    @Log(title = "记录订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RecordOrder recordOrder) {
        String desc = recordOrder.getOrderDescription();
        String[] items = desc.split(";");
        if (items.length != 3) {
            return toAjax(false);
        }
        for (String item : items) {
            String[] its = item.split(":");

        }
        BigDecimal sum = BigDecimal.valueOf(Double.parseDouble(items[2].split(":")[1]));
        recordOrder.setMoney(sum);
        recordOrder.setRecordDate(Date.valueOf(DateUtils.getDate()));
        recordOrder.setRecordTime(Time.valueOf(DateUtils.dateTimeNow("HH:mm:ss")));
        return toAjax(recordOrderService.insertRecordOrder(recordOrder));
    }

    /**
     * 修改记录订单
     */
    @PreAuthorize("@ss.hasPermi('record:order:edit')")
    @Log(title = "记录订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RecordOrder recordOrder) {
        return toAjax(recordOrderService.updateRecordOrder(recordOrder));
    }

    /**
     * 删除记录订单
     */
    @PreAuthorize("@ss.hasPermi('record:order:remove')")
    @Log(title = "记录订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds) {
        return toAjax(recordOrderService.deleteRecordOrderByOrderIds(orderIds));
    }
}
